package com.ytjj.qmyx.supplychain.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.model.request.DelayedRegionRequest;
import com.ytjj.qmyx.supplychain.mapper.DelayedRegionMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxDelayedRegion;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Slf4j
@Service
public class DelayedRegionService {

    @Resource
    private DelayedRegionMapper delayedRegionMapper;

    public CommonResult list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<YxDelayedRegion> list = delayedRegionMapper.selectAll();
        return CommonResult.success(CommonPage.restPage(list));
    }

    public CommonResult update(DelayedRegionRequest request) {
        YxDelayedRegion yxDelayedRegion = new YxDelayedRegion();
        BeanUtils.copyProperties(request,yxDelayedRegion);
        int i = delayedRegionMapper.updateByPrimaryKeySelective(yxDelayedRegion);
        return i > 0 ? CommonResult.success() : CommonResult.failed();
    }

    public String selectIdBy1() {
        YxDelayedRegion yxDelayedRegion = delayedRegionMapper.selectByPrimaryKey(1);
        Map map = new HashMap();
        map.put("delayedReason",yxDelayedRegion.getDelayedReason());
        map.put("region",yxDelayedRegion.getRegion());
        return JSONObject.toJSONString(map);
    }
}
